package com.baizhi.cmfz.service.impl;

import com.baizhi.cmfz.annotation.AddCacheAnnotation;
import com.baizhi.cmfz.annotation.DeleteCacheAnnotation;
import com.baizhi.cmfz.dao.ArticleDao;
import com.baizhi.cmfz.dao.GuruDao;
import com.baizhi.cmfz.entity.Guru;
import com.baizhi.cmfz.service.GuruService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class GuruServiceImpl implements GuruService {
    @Autowired
    private GuruDao guruDao;
    @Autowired
    private ArticleDao articleDao;

    @Override
    @Transactional(readOnly = true)
    @AddCacheAnnotation
    public Map selectAllGuru(int page, int limit, String guruName) {
        page = (page - 1) * limit;
        Map<String, Object> map = new HashMap<>();
        map.put("data", guruDao.selectAllByPage(page, limit, guruName));
        map.put("count", guruDao.countGuru(guruName));
        map.put("code", 0);
        return map;
    }

    @Override
    @DeleteCacheAnnotation
    public int addGuru(Guru guru) {
        guru.setGuruImage("http://localhost:8888/cmfz/" + guru.getGuruImage());
        return guruDao.insertGuru(guru);
    }

    @Override
    @DeleteCacheAnnotation
    public int updateGuru(Guru guru) {
        guru.setGuruImage("http://localhost:8888/cmfz/" + guru.getGuruImage());
        return guruDao.updateGuru(guru);
    }

    @Override
    @DeleteCacheAnnotation
    public int freeze(Guru guru) {
        if (guru.getGuruStatus() == 1) {
            articleDao.updateArticleStatusByGuruId(guru.getGuruId(), 1);
        }
        if (guru.getGuruStatus() == 0) {
            articleDao.updateArticleStatusByGuruId(guru.getGuruId(), 0);
        }
        return guruDao.updateGuru(guru);
    }


    @Override
    @DeleteCacheAnnotation
    public int removeGuru(int[] ids) {
        return guruDao.deleteGuru(ids);
    }

    @Override
    @Transactional(readOnly = true)
    @AddCacheAnnotation
    public Guru selectOneGuru(int guruId) {
        return guruDao.selectGuruById(guruId);
    }

    //所有上师信息
    @Override
    @Transactional(readOnly = true)
    @AddCacheAnnotation
    public List<Guru> showAll() {
        return guruDao.selectAll();
    }
}
